package com.example.dialogdemo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.Person;


/**
 * NotificationActivity
 */
public class NotificationActivity extends AppCompatActivity implements
        View.OnClickListener {

    private View progressBtn;
    private  int progress = 0;
    private NotificationCompat.Builder progressBuilder;


    private NotificationManagerCompat notificationManager;

    private String channelId = "channel_id_01";
    private String channelName = "channel_name_01";

    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


         bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bigpic);

//        NotificationManager notificationManager =
//        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){        //Android 8.0适配
            NotificationChannel channel = new NotificationChannel(channelId,
                    channelName,
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        setContentView(R.layout.activity_notification);
        findViewById(R.id.ac_nf_show_common_btn).setOnClickListener(this);
        findViewById(R.id.ac_nf_show_important_btn).setOnClickListener(this);
        progressBtn = findViewById(R.id.ac_nf_show_progress_btn);
        progressBtn.setOnClickListener(this);

        findViewById(R.id.ac_nf_show_big_txt_btn).setOnClickListener(this);
        findViewById(R.id.ac_nf_show_big_pic_btn).setOnClickListener(this);
        findViewById(R.id.ac_nf_show_message_btn).setOnClickListener(this);
        findViewById(R.id.ac_nf_show_inbox_btn).setOnClickListener(this);
        findViewById(R.id.ac_nf_show_custom_btn).setOnClickListener(this);
        findViewById(R.id.ac_nf_show_decorated_custom_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ac_nf_show_common_btn:
                showCommon();
                break;
            case R.id.ac_nf_show_important_btn:
                showImportant();
                break;
            case R.id.ac_nf_show_progress_btn:
                showProgress();
                break;
            case R.id.ac_nf_show_big_txt_btn:
                showBigTxt();
                break;
            case R.id.ac_nf_show_big_pic_btn:
                showBigPic();
                break;
            case R.id.ac_nf_show_message_btn:
                showMessage();
                break;
            case R.id.ac_nf_show_inbox_btn:
                showInbox();
                break;
            case R.id.ac_nf_show_custom_btn:
                showCustom();
                break;
            case R.id.ac_nf_show_decorated_custom_btn:
                showDecoratedCustom();
                break;
        }
    }




    private void showCommon(){

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this,channelId);

        Intent intent = new Intent(this, NotificationInfoActivity.class);
        int flag = PendingIntent.FLAG_UPDATE_CURRENT;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            flag = flag | PendingIntent.FLAG_IMMUTABLE;
        }
        intent.putExtra("info",System.currentTimeMillis());

        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, intent, flag);

        builder
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .addAction(R.drawable.clean,"事件1",pendingIntent)
                .addAction(R.drawable.clean,"事件2",pendingIntent)
                .addAction(R.drawable.clean,"事件3",pendingIntent)
                .addAction(R.drawable.clean,"事件4",pendingIntent)
                .setContentTitle("Notification普通通知") //指定通知栏的标题内容
                .setContentText("Notification的正文内容," +
                        "通知的正文内容,通知的正文内容,通知的正文内容," +
                        "通知的正文内容,通知的正文内容,通知的正文内容,通知的正文内容!")//通知的正文内容
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.clean))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置

        Notification notification = builder.build() ;

        notificationManager.notify(100, notification);
    }

    private void showImportant(){
        String CHANNEL_ID = "my_channel_02";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){        //Android 8.0适配
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    "my_channel_02_name",
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setShowBadge(true);
            notificationManager.createNotificationChannel(channel);
        }
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this,CHANNEL_ID);

        builder
                .setVisibility(NotificationCompat.VISIBILITY_SECRET)
                .setContentTitle("Notification重要通知") //指定通知栏的标题内容
                .setContentText("Notification的正文内容," +
                        "通知的正文内容,通知的正文内容,通知的正文内容," +
                        "通知的正文内容,通知的正文内容,通知的正文内容,通知的正文内容!")//通知的正文内容
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                .setNumber(12)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.clean))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置

        Notification notification = builder.build() ;

        notificationManager.notify(101, notification);
    }

    private void showProgress() {
        if (progressBuilder == null){
            String CHANNEL_ID = "my_channel_03";
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){        //Android 8.0适配
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                        "my_channel_03_name",
                        NotificationManager.IMPORTANCE_LOW);
                channel.setShowBadge(true);
                notificationManager.createNotificationChannel(channel);
            }
            progressBuilder =
                    new NotificationCompat.Builder(this,CHANNEL_ID);
            progressBuilder
                    .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                    .setContentTitle("Notification进度条通知") //指定通知栏的标题内容
                    .setContentText("进度完成" +progress+"%")//通知的正文内容
                    .setWhen(System.currentTimeMillis())//通知创建的时间
                    .setColor(Color.rgb(255,0,0))
                    .setSound(null)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.clean))
                    .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置
        }else {
            progressBuilder.setProgress(100, progress, false)
                            .setContentText("进度完成" +progress+"%");
        }
        notificationManager.notify(102, progressBuilder.build());
        if (progress <100){
            progress ++;
            progressBtn.postDelayed(new Runnable() {
                @Override
                public void run() {
                    showProgress();
                }
            },100);
        }else {
            notificationManager.cancel(102);
        }
    }


    private void showBigTxt() {

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this,channelId);

        builder
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                .setContentTitle("Notification大文本通知") //指定通知栏的标题内容
                .setStyle(new NotificationCompat.BigTextStyle().bigText("大文本通知的正文内容," +
                        "通知的正文内容,通知的正文内容,通知的正文内容," +
                        "通知的正文内容,通知的正文内容,通知的正文内容,通知的正文内容!"))
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置

        Notification notification = builder.build() ;

        notificationManager.notify(103, notification);
    }


    private void showBigPic() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this,channelId);
        builder
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                .setContentTitle("Notification大图片通知") //指定通知栏的标题内容
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置

        Notification notification = builder.build() ;

        notificationManager.notify(104, notification);
    }


    private void showMessage() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this,channelId);
        builder
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                .setContentTitle("Notification消息通知") //指定通知栏的标题内容
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置


        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P){
            Bundle bundle1 = new Bundle();
            bundle1.putString("name","Tom");

            Bundle bundle2 = new Bundle();
            bundle2.putString("name","Jack");

            Bundle bundle3 = new Bundle();
            bundle3.putString("name","Been");
            builder.setStyle(new NotificationCompat.MessagingStyle(Person.fromBundle(bundle1))
                    .addMessage("message 1",System.currentTimeMillis(),
                           Person.fromBundle(bundle2))
                    .addMessage("message 2",System.currentTimeMillis(),
                            Person.fromBundle(bundle3))
            );
        }

        Notification notification = builder.build() ;

        notificationManager.notify(105, notification);
    }

    private void showInbox() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this,channelId);
        builder
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                .setContentTitle("Notification Inbox通知") //指定通知栏的标题内容
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.addLine("line 1").addLine("line 2");
        builder.setStyle(inboxStyle);

        Notification notification = builder.build() ;

        notificationManager.notify(106, notification);
    }


    private void showCustom() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this,channelId);
        builder
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                .setContentTitle("Notification 自定义通知") //指定通知栏的标题内容
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置
//
        RemoteViews views = new RemoteViews(this.getPackageName(),
                R.layout.rview_nf);

        RemoteViews bigViews = new RemoteViews(this.getPackageName(),
                R.layout.rview_nf_big);

        Intent intent = new Intent(this, NotificationInfoActivity.class);
        int flag = PendingIntent.FLAG_UPDATE_CURRENT;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            flag = flag | PendingIntent.FLAG_IMMUTABLE;
        }
        intent.putExtra("info",System.currentTimeMillis());

        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, intent, flag);

        bigViews.setOnClickPendingIntent(R.id.rv_nf_ok,pendingIntent);

//        builder.setContent(bigViews);
        builder.setCustomContentView(bigViews);
//        builder.setCustomBigContentView(bigViews);
//        builder.setCustomHeadsUpContentView(bigViews);

        Notification notification = builder.build() ;

        notificationManager.notify(107, notification);
    }

    private void showDecoratedCustom() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this,channelId);
        builder
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                .setContentTitle("Notification DecoratedCustom通知") //指定通知栏的标题内容
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置

        NotificationCompat.DecoratedCustomViewStyle decoratedCustomViewStyle =
                new NotificationCompat.DecoratedCustomViewStyle();

        RemoteViews views = new RemoteViews(this.getPackageName(),
                R.layout.rview_nf);

        RemoteViews bigViews = new RemoteViews(this.getPackageName(),
                R.layout.rview_nf_big);

        builder.setCustomContentView(views);
        builder.setCustomBigContentView(bigViews);

        builder.setStyle(decoratedCustomViewStyle);

        Notification notification = builder.build() ;

        notificationManager.notify(108, notification);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmap != null){
            bitmap.recycle();
        }

    }
}